import { Message } from 'iview';
import ajax from './ajax';

const ajaxiView = new Object();

function handleResponse(response) {
  if(response.status != 200) {
    Message.error('Status: ' + response.status + ', Message: ' + response.message);
    return false;
  } else if(response.data.code != 0) {
    Message.warning('Message: ' + response.data.message);
    return false;
  } else {
    return response.data;
  }
}

function handleCatch(error) {
  Message.error('Status: ' + error.status + ', Message: ' + error.message);
  return false;
};

ajaxiView.get = function(url) {
  return ajax.get(url).then(handleResponse).catch(handleCatch);
}

ajaxiView.post = function(url, param={}) {
  return ajax.post(url, param).then(handleResponse).catch(handleCatch);
}

ajaxiView.put = function(url, param={}) {
  return ajax.put(url, param).then(handleResponse).catch(handleCatch);
}

ajaxiView.delete = function(url) {
  return ajax.delete(url).then(handleResponse).catch(handleCatch);
}

export default ajaxiView;
